﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace HROReporting.Infrastructure.Authentication
{
    public class AspMembershipAuthentication : ILocalAuthenticationService
    {
        public User Login(string email, string password)
        {
            User user = new User();
            user.IsAuthenticated = false;

            if (Membership.ValidateUser(email, password))
            {
                MembershipUser validatedUser = Membership.GetUser(email);
                
                user.AuthenticationToken = validatedUser.ProviderUserKey.ToString();
                user.Email = email;
                user.IsAuthenticated = true;
            }

            return user;
        }

        public User RegisterUser(string email, string password, string userType)
        {
            MembershipCreateStatus status;
            User user = new User();
            user.IsAuthenticated = false;

            if (userType == "student")
            {
                CreateStudentRole();

                if (!new StudentEmailSpecification().IsSatisfiedBy(email))
                {
                    throw new InvalidOperationException("Vul a.u.b. uw studentenmail in.");
                }
            }

            if (userType == "docent")
            {
                CreateDocentRole();
            }

            Membership.CreateUser(email, password, email, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), true, out status);
            

            if (status == MembershipCreateStatus.Success)
            {
                MembershipUser newlyCreatedUser = Membership.GetUser(email);
                if (userType == "student")
                {
                    Roles.AddUserToRole(newlyCreatedUser.UserName, "Student");
                }
                else if (userType == "docent")
                {
                    Roles.AddUserToRole(newlyCreatedUser.UserName, "Docent");
                }
                user.AuthenticationToken = newlyCreatedUser.ProviderUserKey.ToString();

                user.Email = email;
                user.IsAuthenticated = true;
            }
            else
            {
                switch (status)
                {
                    case MembershipCreateStatus.DuplicateEmail:
                        throw new InvalidOperationException(
                                "Er bestaat al een gebruiker met dit emailadres.");
                    case MembershipCreateStatus.DuplicateUserName:
                        throw new InvalidOperationException(
                                "Er bestaat al een gebruiker met dit emailadres.");
                    case MembershipCreateStatus.InvalidEmail:
                        throw new InvalidOperationException(
                                "Uw emailadres is niet geldig.");
                    default:
                        throw new InvalidOperationException(
                                "Er is een probleem ontstaan bij het aanmaken van uw account . " +
                                "Probeer het nog eens.");
                }
            }

            return user;
        }

        private void CreateDocentRole()
        {
            if (!Roles.RoleExists("Docent"))
            {
                Roles.CreateRole("Docent");
            }
        }

        public void CreateStudentRole()
        {
            if (!Roles.RoleExists("Student"))
            {
                Roles.CreateRole("Student");
            }
        }
    }
}
